package com.foreveross.fetl.core.domain.freight;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.dayatang.domain.InstanceFactory;
import com.foreveross.fetl.common.domain.BaseEntity;
import com.foreveross.fetl.core.domain.freight.repository.ITaskTimeRepository;



@Entity
@Table(name="task_time")
public class TaskTimeEntity extends BaseEntity {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1220222133466028692L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Column(name="CHANNEL")
	private String channel;
	
	/**
	 * 预处理code
	 */
	@Column(name="TASK_CODE")
	private String taskCode;
	
	/**
	 * 预处理名字
	 */
	@Column(name="TASK_NAME")
	private String taskName;
	
	/**
	 * 在哪个小时里触发
	 */
	@Column(name="HOUR")
	private Integer hour;
	
	/**
	 * 抓取时间从哪个小时开始
	 */
	@Column(name="GRAB_START")
	private Integer grabStart;
	
	/**
	 * 抓取时间到哪个小时结束
	 */
	@Column(name="GRAB_STOP")
	private Integer grabStop;
	
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	

	public String getChannel() {
		return channel;
	}

	public void setChannel(String channel) {
		this.channel = channel;
	}

	public String getTaskCode() {
		return taskCode;
	}

	public void setTaskCode(String taskCode) {
		this.taskCode = taskCode;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public Integer getHour() {
		return hour;
	}

	public void setHour(Integer hour) {
		this.hour = hour;
	}

	public Integer getGrabStart() {
		return grabStart;
	}

	public void setGrabStart(Integer grabStart) {
		this.grabStart = grabStart;
	}

	public Integer getGrabStop() {
		return grabStop;
	}

	public void setGrabStop(Integer grabStop) {
		this.grabStop = grabStop;
	}
	
	public ITaskTimeRepository getITaskTimeRepository(){
		return InstanceFactory.getInstance(ITaskTimeRepository.class);
	}
	
	public List<TaskTimeEntity> findByCode(String channel,String code) {
		return getITaskTimeRepository().findByCode(channel,code);
	}
	
	public List<TaskTimeEntity> findByCondition(String channel,String code, Integer hour) {
		return getITaskTimeRepository().findByCondition(channel,code, hour);
	}
	
	public void saveOrUpdate(TaskTimeEntity entity)  {
		
			if(null==entity.getHour()){
				throw new IllegalArgumentException("处理时间不能为空");
			}
			if(null==entity.getChannel()){
				throw new IllegalArgumentException("渠道不能为空");
			}
			if(null==entity.getTaskCode()){
				throw new IllegalArgumentException("处理类型code不能为空");
			}
			if(null==entity.getId() ){
				List<TaskTimeEntity> list =findByCondition(entity.getChannel(),entity.getTaskCode(),entity.getHour());
				if(null!=list && list.size()>0){
					throw new IllegalArgumentException(entity.getChannel()+" "+entity.getTaskCode()+" "+entity.getHour()+"已存在");
				}
			}
		
		getITaskTimeRepository().save(entity);
	}
	
	public void delete(TaskTimeEntity entity){
		getITaskTimeRepository().delete(entity);
	}
	
	public TaskTimeEntity get(Long id){
		return getITaskTimeRepository().find(TaskTimeEntity.class, id);
	}

}
